package com.ruoyi.tranShort.mqtt.request;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.mqtt.api.MqttClientApi;
import com.ruoyi.tranShort.mqtt.CheckUtil;
import com.ruoyi.tranShort.mqtt.ResponseHandle;
import com.ruoyi.tranShort.mqtt.api.GatewayRequest;
import com.ruoyi.tranShort.mqtt.common.constants.ControllerNumberConstants;
import com.ruoyi.tranShort.mqtt.common.constants.TypeConstants;
import com.ruoyi.tranShort.mqtt.entity.ComType;
import com.ruoyi.tranShort.mqtt.entity.Content;
import com.ruoyi.tranShort.mqtt.entity.RequestBase;
import com.ruoyi.tranShort.mqtt.exception.ResponseTimeOutException;
import com.ruoyi.tranShort.mqtt.request.entity.SendMessageData;
import com.ruoyi.tranShort.mqtt.response.SettingResponse;
import com.ruoyi.tranShort.utils.MqttClientUtil;

import java.util.HashMap;
import java.util.Map;

/**
 * 设置接口
 * @author wwy
 * @date 2021-08-11 17:04
 */
public class SettingRequest implements GatewayRequest {
    private RequestBase requestBase = new RequestBase();
    private ComType comType = new ComType();
    private Content content = new Content();

    public SettingRequest() {
        requestBase.setComType(comType);
        comType.setClientId("");
        comType.setMsgId(System.currentTimeMillis()+"");
        comType.setType(TypeConstants.DOWN);
        comType.setContent(content);
        content.setType(TypeConstants.PASSTHROUGH);
    }
    private String mac;
    private String pubTopic;
    private int qos;
    /**
     * key为要设置的编号，右边为设置的类型值
     */
    private Map<String,String> settingMap;
    @Override
    public SettingResponse execute() {
        SettingResponse settingResponse = new SettingResponse();
        settingResponse.setCode(500);
        SendMessageData sendMessageData = new SendMessageData();
        sendMessageData.setMac(mac);
        if(settingMap == null || settingMap.size()<=0){
            return settingResponse;
        }
        //key value组合
        StringBuilder settingNumBuilder = new StringBuilder();
        settingNumBuilder.append("00");
        for (Map.Entry<String, String> settingEntity : settingMap.entrySet()) {
            String value = settingEntity.getValue();
            String key = settingEntity.getKey();
            settingNumBuilder.append(key).append(value);
        }
        //数据长度计算
        String settingNumStr = settingNumBuilder.toString();
        int length = settingNumStr.length() / 2;
        String lengthHex = Integer.toHexString(length);
        //高低字节转换
        while (lengthHex.length()<4){
            lengthHex = "0"+lengthHex;
        }
        //长度
        String lengthHexStr = lengthHex.substring(2)+lengthHex.substring(0,2);
        //校验码
        String message1 = ControllerNumberConstants.SETTING+lengthHexStr+settingNumStr;
        String checkSum  = CheckUtil.makeChecksum(message1);
        String lastMessage = ControllerNumberConstants.SETTING+lengthHexStr+settingNumStr + checkSum+"16";
        sendMessageData.setMac(mac);
        sendMessageData.setData(lastMessage);
        content.setData(sendMessageData);
        //发送消息
        MqttClientApi mqttClientApi = MqttClientUtil.getMqttClientApi();
        String message = JSONObject.toJSONString(requestBase);
        mqttClientApi.publishMessage(pubTopic,message,qos);
        try {
            String waitMessage = ResponseHandle.waitMessage(mac);
            //解析响应信息
            String conStr = waitMessage.substring(0, 4);
            if(ControllerNumberConstants.REPLY_SETTING.equals(conStr)){
                //成功
                settingResponse.setCode(200);
            }
        } catch (ResponseTimeOutException e) {
            settingResponse.setCode(500);
        }
        return settingResponse;
    }

    public String getMac() {
        return mac;
    }

    public void setMac(String mac) {
        this.mac = mac;
    }

    public String getPubTopic() {
        return pubTopic;
    }

    public void setPubTopic(String pubTopic) {
        this.pubTopic = pubTopic;
    }

    public int getQos() {
        return qos;
    }

    public void setQos(int qos) {
        this.qos = qos;
    }

    public Map<String, String> getSettingMap() {
        return settingMap;
    }

    public void setSettingMap(Map<String, String> settingMap) {
        this.settingMap = settingMap;
    }
}
